package com.scs.application.modules.fsd.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.modules.fsd.dto.SaveFpTempDTO;
import com.scs.application.modules.fsd.dto.SaveUserDTO;
import com.scs.application.modules.fsd.entity.FsdUser;
import com.scs.application.modules.fsd.mapper.FsdUserMapper;
import com.scs.application.modules.fsd.service.FsdUserService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/back_front/users")
public class FsdUserController extends CrudController<FsdUserService, FsdUser> {
    @Autowired
    private FsdUserService userService;

    @Autowired
    private FsdUserMapper userMapper;


    @GetMapping(value = "/getAllService")
    public RestResult findAll(){
        return findAll(false);
    }

    /**
     * 获取数据列表
     */
    @Override
    @GetMapping(value ="/getPage" )
    public PageResult page(){
        QueryContext queryContext = QueryContextHolder.currentContext();
        IPage<HashMap<String,Object>> pageResult = userMapper.getUserList(queryContext.getPage(), queryContext.getWrapper());
        return PageResult.ok("get User list success", pageResult.getRecords(), pageResult.getTotal());
    }

    /**
     * 获取数据列表
     */
    @GetMapping(value ="/getService" )
    public RestResult getUserList(@RequestParam("keyword") String keyword, @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize){
        Map<String, Object> users= userService.getUsers(keyword,currentPage,pageSize);
        return RestResult.ok("get User list success",users);
    }

    @GetMapping(value ="/synchronizeUsersService" )
    public RestResult synchronizeUsersService(){
        if(userService.synchronizeUsers())
            return RestResult.ok("用户信息同步完成");
        return RestResult.error("用户信息同步失败");
    }

    @GetMapping(value ="/getRelationByUserService" )
    public RestResult getRelationByUser(@RequestParam("id") String id) {
        return RestResult.ok("获取用户相关所有关系成功",service.getRelationByUser(id));
    }

    @GetMapping(value ="/getFpTempByUserService" )
    public RestResult getFpTempByUser(@RequestParam("userId") String userId) {
        return RestResult.ok("获取用户所有指纹模板成功",service.getFpTempByUser(userId));
    }

    @PostMapping(value = "/createService")
    public RestResult createUser(@RequestBody @Valid SaveUserDTO userDTO) {
        boolean isOk = userService.createUser(userDTO);
        if(isOk)
            return RestResult.ok("保存成功！");
        return RestResult.error("保存失败");
    }

    @PostMapping(value = "/createFpTempService")
    public RestResult createFpTemp(@RequestBody Map<String, String> map) {
        boolean isOk = userService.createFpTemp(map.get("userId"));
        if(isOk)
            return RestResult.ok("添加指纹模板成功！");
        return RestResult.error("添加指纹模板失败!");
    }

    @PostMapping(value = "/editService")
    public RestResult editUser(@RequestBody @Valid SaveUserDTO userDTO) {
        boolean isOk = userService.editUser(userDTO);
        if(isOk)
            return RestResult.ok("保存成功！");
        return RestResult.error("保存失败");
    }

    @PostMapping(value = "/editFpTempService")
    public RestResult editFpTemp(@RequestBody @Valid List<SaveFpTempDTO> fpTempDTOS) {
        boolean isOk = userService.editFpTemp(fpTempDTOS);
        if(isOk)
            return RestResult.ok("保存成功！");
        return RestResult.error("保存失败");
    }


    @DeleteMapping(value = "back_front/validate/logout")
    public RestResult frontLogout( @RequestHeader("Intellic-configuration") String config) {
        userService.logout(JSONObject.parseObject(config).getString("stationCode"));
        return RestResult.ok("退出登录成功");
    }
    /**
     * 上传头像
     * @param file
     * @param userId
     * @return
     */
    @PostMapping("/uploadPhoto")
    public RestResult uploadPhoto(@RequestParam("file") MultipartFile file, @RequestParam("userId") String userId) {
        return RestResult.ok("上传文件成功!", service.uploadPhoto(file, userId));
    }

//    @PassToken
//    @GetMapping(value = "/user/login")
//    public RestResult login(@Valid LoginParam loginParam) {
//        LoginResult loginResult = userService.login(loginParam.getJobNumber(), loginParam.getPassword());
//        return RestResult.ok("登录成功",loginResult);
//    }


}
